package com.csw.android.player.exoplayer

import android.os.Looper
import com.csw.android.dev_utils.SDK
import com.google.android.exoplayer2.C
import com.google.android.exoplayer2.DefaultRenderersFactory
import com.google.android.exoplayer2.ExoPlayer
import com.google.android.exoplayer2.extractor.DefaultExtractorsFactory
import com.google.android.exoplayer2.extractor.ts.DefaultTsPayloadReaderFactory.FLAG_ALLOW_NON_IDR_KEYFRAMES
import com.google.android.exoplayer2.extractor.ts.DefaultTsPayloadReaderFactory.FLAG_DETECT_ACCESS_UNITS
import com.google.android.exoplayer2.source.DefaultMediaSourceFactory

/**
 * exo播放器实例获取
 */
object ExoplayerFactory {

    /**
     * 创建通用的ExoPlayer实例
     */
    fun createSimpleExoPlayer(): ExoPlayer {
        return ExoPlayer.Builder(
            SDK.getApplication(),
            DefaultRenderersFactory(SDK.getApplication()).apply {
                setExtensionRendererMode(DefaultRenderersFactory.EXTENSION_RENDERER_MODE_PREFER)
            },
            DefaultMediaSourceFactory(SDK.getApplication(),
                DefaultExtractorsFactory().apply {
                    setTsExtractorFlags(FLAG_DETECT_ACCESS_UNITS or FLAG_ALLOW_NON_IDR_KEYFRAMES)
                })
        )
            .setWakeMode(C.WAKE_MODE_NETWORK)
            .setLooper(Looper.getMainLooper())
            .build()
    }

}